package cn.silver.module.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.silver.framework.log.annotation.Log;
import cn.silver.framework.log.enums.OperateEnums;
import cn.silver.framework.mybatis.controller.DataController;
import cn.silver.framework.openapi.model.Result;
import cn.silver.module.system.domain.SysUser;
import cn.silver.module.system.bean.UserBean;
import cn.silver.module.system.param.UserParam;
import cn.silver.module.system.service.ISysUserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息
 *
 * @author Silver Zhou
 */
@Validated
@RestController
@RequiredArgsConstructor
@Tag(name = "系统管理-用户管理")
@RequestMapping("/system/user")
public class SysUserController extends DataController<ISysUserService, SysUser, UserParam, UserBean> {

    @Resource
    @Override
    public void setBaseService(ISysUserService baseService) {
        this.setPermission("system:user");
        this.setCode("system_user");
        this.setTitle("系统管理-用户管理");
        this.baseService = baseService;
    }

    /**
     * 重置密码
     */
    @SaCheckPermission("system:user:resetPwd")
    @Log(title = "用户管理", operate = OperateEnums.ALTER_UPDATE)
    @PostMapping("/resetPwd/{userIds}")
    public Result<Void> resetPwd(@PathVariable String[] userIds) {
//        baseService.checkUserAllowed(user.getType());
//        baseService.checkUserDataScope(user.get());
//        user.setPassword(BCrypt.hashpw(user.getPassword()));
        baseService.resetUserPwd(List.of(userIds));
        return Result.ok();
    }

    /**
     * 状态修改
     */
    @SaCheckPermission("system:user:edit")
    @Log(title = "用户管理", operate = OperateEnums.ALTER_UPDATE)
    @PostMapping("/changeStatus")
    public Result<Void> changeStatus(@RequestBody UserParam user) {
//        baseService.checkUserAllowed(user.getUserType());
//        baseService.checkUserDataScope(user.getUserId());
        baseService.updateUserStatus(user.getId(), user.getStatus());
        return Result.ok();
    }
//    /**
//     * 导出用户列表
//     */
//    @Log(title = "用户管理", operate = OperateType.EXPORT)
//    @SaCheckPermission("system:user:export")
//    @PostMapping("/export")
//    public void export(UserParam user, HttpServletResponse response) {
//        List<UserModel> list = baseService.selectList(user);
//        List<SysUserExportVo> listVo = MapstructUtils.convert(list, SysUserExportVo.class);
//        ExcelUtil.exportExcel(listVo, "用户数据", SysUserExportVo.class, response);
//    }
//
//    /**
//     * 导入数据
//     *
//     * @param file          导入文件
//     * @param updateSupport 是否更新已存在数据
//     */
//    @Log(title = "用户管理", operate = OperateType.IMPORT)
//    @SaCheckPermission("system:user:import")
//    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
//    public Result<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
//        ExcelResult<SysUserImportVo> result = ExcelUtil.importExcel(file.getInputStream(), SysUserImportVo.class, new SysUserImportListener(updateSupport));
//        return Result.ok(result.getAnalysis());
//    }
//
//    /**
//     * 获取导入模板
//     */
//    @PostMapping("/importTemplate")
//    public void importTemplate(HttpServletResponse response) {
//        ExcelUtil.exportExcel(new ArrayList<>(), "用户数据", SysUserImportVo.class, response);
//    }
}
